<?php
/**
 * ClassName:标题处理器
 * @auditor:leijunxiang
 * @time 2025/4/8 14:31
 */

namespace app\tool\word;

use app\factory\Factory;
use app\modelOperate\freightForward\ChannelDetailOpe;
use app\modelOperate\freightForward\ChannelOpe;
use app\modelOperate\freightForward\FreightForwardOpe;
use app\tool\ToolBase;
use think\facade\Log;

class TitleHandle extends ToolBase
{
    //标题定位对象
    protected $titlePositionObj;
    //excel对象
    protected $excelObj;
    //表格数据对象
    protected $tableDataObj;

    //(true/false)开始处理(标题定位对象,excel对象,表格数据对象,货代id)
    public function startHandle($titlePositionObj, $excelObj, $tableDataObj, $forwardId)
    {
        if ($titlePositionObj && $excelObj && $tableDataObj) {
            //初始化数据
            $this->initData($titlePositionObj, $excelObj, $tableDataObj);
            //重量处理
            $this->weightHandle();
            //保存数据对象
            $this->saveDataObj($forwardId);
            //清除数据
            $this->cleanData();
        } else {
            $this->errCode = FAIL;
            $this->errMsg = "数据未初始化,暂不执行";
        }
        return $this->isGoOn();
    }

    //初始化数据
    protected function initData($titlePositionObj, $excelObj, $tableDataObj)
    {
        $this->init();
        //标题定位对象
        $this->titlePositionObj = $titlePositionObj;
        //excel对象
        $this->excelObj = $excelObj;
        //表格数据对象
        $this->tableDataObj = $tableDataObj;
    }

    //重量处理
    protected function weightHandle()
    {
        if (!$this->isGoOn()) {
            return;
        }
        //取出重量开始坐标和结束坐标
        $startCoord = $this->titlePositionObj->getWeightStartCoord();
        $endCoord = $this->titlePositionObj->getWeightEndCoord();
        $arr = [];
        $tool = app(WordHandleTool::class);
        //循环列
        for ($col = $startCoord[1]; $col <= $endCoord[1]; $col++) {
            //取出坐标对应的值
            //(字符串)开始处理(excel对象,行,列)
            $cellValue = $tool->startHandle($this->excelObj, $startCoord[0], $col);
            // 获取单元格的值
//            $cellValue = $this->excelObj->getCell($col . $startCoord[0])->getValue();
            $arr[] = $cellValue;
        }
        $this->tableDataObj->setWeightList($arr);
    }

    //保存数据对象
    protected function saveDataObj($forwardId)
    {
        if (!$this->isGoOn()) {
            return;
        }

        //(货代id)保存货代信息
//        $forwardId = $this->saveFreightForwardMsg();
        //(渠道id)保存渠道信息(货代id)
        $channelId = $this->saveChannelMsg($forwardId);
        //保存渠道详情信息(渠道id)
//        $this->saveChannelDetailMsg($channelId);
    }

    //清除数据
    protected function cleanData()
    {
        $this->titlePositionObj = null;
        $this->excelObj = null;
        $this->tableDataObj = null;
    }

    //(货代id)保存货代信息
    protected function saveFreightForwardMsg()
    {
        if (!$this->isGoOn()) {
            return;
        }
        if ($this->tableDataObj->getForwardId()) {
            return 0;
        }
        // 获取货代名称
        $tool = app(WordHandleTool::class);
        //(字符串)开始处理(excel对象,行,列)
        $cellValue = $tool->startHandle($this->excelObj, $this->titlePositionObj->getFreightForwardCoord[0], $this->titlePositionObj->getFreightForwardCoord[1]);
//        $cellValue = $this->excelObj->getCell($this->titlePositionObj->getFreightForwardCoord[1] . $this->titlePositionObj->getFreightForwardCoord[0])->getValue();
        //保存货代名称
        $this->tableDataObj->setName($cellValue);
        //保存数据
        $ope = app(FreightForwardOpe::class);
        $forwardId = Factory::createUniqueNumber();

        $this->tableDataObj->setForwardId($forwardId);
        $arr = [
            //货代id
            "forwardId" => $forwardId,
            //货代名称
            "name" => $cellValue
        ];
        $flag = $ope->saveData($arr);
        if (!$flag) {
            $this->errMsg = "保存货代失败,请重试";
            $this->errCode = FAIL;
            return 0;
        }
        return $forwardId;
    }

    //(渠道id)保存渠道信息(货代id)
    protected function saveChannelMsg($forwardId)
    {
        if (!$this->isGoOn()) {
            return 0;
        }
        $tool = app(WordHandleTool::class);
        $ope = app(ChannelOpe::class);
        //获取渠道名称
        $cellValue = $tool->startHandle($this->excelObj, $this->titlePositionObj->getChannelCoord()[0], $this->titlePositionObj->getChannelCoord()[1]);
//        $cellValue = $this->excelObj->getCell($this->titlePositionObj->getChannelCoord()[1] . $this->titlePositionObj->getChannelCoord()[0])->getValue();
        //根据渠道名称查询数据(渠道名称,货代id)
        $data = $ope->selectDataByChannelName($cellValue,$forwardId);
        if (@$data['channelId']) {
            $this->tableDataObj->setChannelId($data['channelId']);
            return 0;
        }
        //保存渠道名称
        $this->tableDataObj->setChannelName($cellValue);
        $channelId = Factory::createUniqueNumber();
        //保存渠道id
        $this->tableDataObj->setChannelId($channelId);

        $arr = [
            //渠道id
            "channelId" => $channelId,
            //货代id
            "forwardId" => $forwardId,
            //渠道名称
            "channelName" => $cellValue,
        ];
        $flag = $ope->saveData($arr);
        if (!$flag) {
            $this->errMsg = "保存渠道失败,请重试";
            $this->errCode = FAIL;
            return 0;
        }

        return $channelId;
    }

    //保存渠道详情信息(渠道id)
    protected function saveChannelDetailMsg($channelId)
    {
        if (!$this->isGoOn()) {
            return;
        }
        $tool = app(WordHandleTool::class);
        //(字符串)开始处理(excel对象,行,列)
        $cellValue = $tool->startHandle($this->excelObj, $this->titlePositionObj->getAreaCoord[0], $this->titlePositionObj->getAreaCoord[1]);
        //获取区域名称
//        $cellValue = $this->excelObj->getCell($this->titlePositionObj->getAreaCoord[1] . $this->titlePositionObj->getAreaCoord[0])->getValue();
        //保存区域名称
        $this->tableDataObj->setAreaName($cellValue);
        $ope = app(ChannelDetailOpe::class);
        $arr = [
            //渠道详情id
            "detailId" => Factory::createUniqueNumber(),
            //渠道id
            "channelId" => $channelId,
            //区域名称
            "area" => $cellValue
        ];
        $flag = $ope->saveData($arr);
        if (!$flag) {
            $this->errMsg = "保存仓库信息失败,请重试";
            $this->errCode = FAIL;
            return;
        }
    }

}